package com.example.api.utils;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;

public final class RandomUtil {

    public static String next() {
        StringBuilder builder = new StringBuilder();
        Random random = new Random();
        for (int i = 0; i < 6; i++) {
            builder.append(random.nextInt(10));
        }
        return builder.toString();
    }

    /**
     * 生成指定前缀，指定长度的随机数
     * @param prefix 前缀
     * @param length 长度
     * @return String
     */
    public static String getRandomNum(String prefix, int length) {
        String base = "0123456789";
        Random random = new Random();
        Date date = new Date();
        StringBuilder sb = new StringBuilder(prefix);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
        String time = sdf.format(date);
        sb.append(time);
        int charL = length - sb.toString().length();
        for (int i = 0; i < charL; i++) {
            int number = random.nextInt(base.length());
            sb.append(base.charAt(number));
        }
        return sb.toString();
    }

}
